/**
 * @author liu_j@worksap.co.jp
 */
Ext.define('Goban.util.Request', {
	statics: {
		getNextMove: function(goban, goType, aiType, aiLevel, onSuccess, onFailure) {
			var param = {
				x: goban.length,
				y: goban[0].length,
				black: [],
				white: [],
				gotype: goType,
				aitype: aiType,
				ailevel: aiLevel
			};
			for(var i=0; i<goban.length; i++) {
				for(var j=0; j<goban[i].length; j++) {
					if(goban[i][j]===-1) {
						param.black.push([i,j]);
					} else if (goban[i][j]===1) {
						param.white.push([i,j]);
					}
				}
			}
			var url = 'rest/nextmove/get';
			var queryString = Goban.util.Request.requestParamToUriString(param);
			Ext.Ajax.timeout = 30 * 60 * 1000;
			Ext.Ajax.request({
				url: url+'?'+queryString,
				method: 'GET',
				success: onSuccess,
				failure: onFailure
			});
		},
		requestParamToUriString: function(params) {
			var result = '';
			var count = 0;
			for (arg in params) {
				if (count != 0) {
					result += '&';
				}
				var value = params[arg];
				if(Ext.isObject(value) || Ext.isArray(value)) {
					result += encodeURIComponent(arg) + '=' + Ext.encode(value);
				} else {
					result += encodeURIComponent(arg) + '=' + encodeURIComponent(value);
				}
				count++;
			}
			return result;
		}
	}
});